/**
	Constructs a Rectangle

	@author Matthew Richmond
	@version 2006-05-25

	@usage

	- Activate with:
	<code>

	</code>

	@notes based upon the insight of mc.drawRect by Ric Ewing (ric@formequalsfunction.com), thanks!

*/
import timber.drawing.Shape;

class timber.drawing.Rectangle extends Shape{

// ===========================================================
// - CONSTRUCTOR
// ===========================================================

	function Rectangle (mc:MovieClip ) {
		super(mc)
		//trace("Rectangle");
	};

// ===========================================================
// - ACESSORS
// ===========================================================

	function draw (x:Number, y:Number, w:Number, h:Number, cornerRadius:Number){
		//trace("draw");
		
		mc.clear();
		
		if (fill) mc.beginFill(fillColor, fillAlpha)
		if (stroke) mc.lineStyle(1,strokeColor, strokeAlpha)
		
		if (cornerRadius > 0) {
			
			// init vars
			var theta, angle, cx, cy, px, py;
			// make sure that w + h are larger than 2*cornerRadius
			if (cornerRadius>Math.min(w, h)*.5) {
				cornerRadius = Math.min(w, h)*.5;
			}
			// theta = 45 degrees in radians
			theta = Math.PI/4;
			// draw top line
			mc.moveTo(x+cornerRadius, y);
			mc.lineTo(x+w-cornerRadius, y);
			//angle is currently 90 degrees
			angle = -Math.PI*.5;
			// draw tr corner in two parts
			cx = x+w-cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			angle += theta;
			cx = x+w-cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			// draw right line
			mc.lineTo(x+w, y+h-cornerRadius);
			// draw br corner
			angle += theta;
			cx = x+w-cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+h-cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			angle += theta;
			cx = x+w-cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+h-cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+w-cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			// draw bottom line
			mc.lineTo(x+cornerRadius, y+h);
			// draw bl corner
			angle += theta;
			cx = x+cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+h-cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			angle += theta;
			cx = x+cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+h-cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+h-cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			// draw left line
			mc.lineTo(x, y+cornerRadius);
			// draw tl corner
			angle += theta;
			cx = x+cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
			angle += theta;
			cx = x+cornerRadius+(Math.cos(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			cy = y+cornerRadius+(Math.sin(angle+(theta*.5))*cornerRadius/Math.cos(theta*.5));
			px = x+cornerRadius+(Math.cos(angle+theta)*cornerRadius);
			py = y+cornerRadius+(Math.sin(angle+theta)*cornerRadius);
			mc.curveTo(cx, cy, px, py);
		
		} else {
			
			// cornerRadius was not defined or = 0. mc makes it easy.
			mc.moveTo(x, y);
			mc.lineTo(x+w, y);
			mc.lineTo(x+w, y+h);
			mc.lineTo(x, y+h);
			mc.lineTo(x, y);
			
		}
	
		if (fill) mc.endFill;
	
	};

};